package com.glamour.hlcity.module.dom.analysis.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import com.glamour.hlcity.common.model.Node;
import com.glamour.hlcity.common.util.DownLoadUtil;
import com.glamour.hlcity.common.util.queue.CircleQueue;
import com.glamour.hlcity.module.dom.analysis.DomAnalysis;
import com.glamour.hlcity.module.spider.factory.BeanFactory;
import com.glamour.hlcity.modules.spider.parser.DomParser;

public class HtmlDomAnalysis implements DomAnalysis {
	/**
	 * 编码方式
	 */
	public final static String ENCODING_GB2312 = "gb2312";
	public final static String ENCODING_GBK = "gbk";
	public final static String ENCODING_UTF8 = "utf8";

	@Override
	public List<Node> produceNodes(String url) {
		DomParser parser = BeanFactory
				.createDomParser(DomParser.BEAN_NAME_HTML_DOM_PARSER);
		return parser.parser(parser.analysis(url));
	}

	public static void main(String[] args) {
		String[] strs = new String[] { "http://www.neu.edu.cn/neupic",
				};
		String dir = "e:/temp/file_aliabab";
		for(int i=0;i<strs.length;i++){
			dir=dir+i;
			DownLoadUtil.mkdir(dir);
			siteAnalysis(strs[i], dir);
		}

	}

	private static void siteAnalysis(String site, String dir) {
		Long time = System.currentTimeMillis();
		HashSet<String> links = new HashSet<String>();
		CircleQueue queue = new CircleQueue(10000);
		Node node = new Node();
		// node.setNodeNo("http://www.zuirt.com/");
		node.setNodeNo(site);
		queue.addNode(node);
		int max = 100;
		int i = 0;
		Node temp;
		while (i < max) {
			i++;
			temp = queue.topNode();
			if (temp == null) {
				continue;
			}
			if (links.add(temp.getNodeNo())) {
				queue.addNodes(executeLink(temp.getNodeNo(), dir));
			}
			System.out.println("##################################" + i);
		}
		System.out.println("用时:" + (System.currentTimeMillis() - time));
	}

	private static List<Node> executeLink(String url, String dir) {
		List<Node> result = new ArrayList<Node>();
		DomAnalysis analysis = BeanFactory
				.createDomAnalysis(DomAnalysis.BEAN_HTML_DOMANALYSIS);
		List list = analysis.produceNodes(url);
		// String dir="e:/temp/file2/";
		String filePath = "";
		Integer imgSize = 0;
		for (int i = 0; i < list.size(); i++) {
			Node node = (Node) list.get(i);
			if (node.getNodeType().equals(Node.LINK_NODE_TYPE_IMG)) {
				filePath = dir.concat("/" + System.currentTimeMillis() + "");
				filePath = filePath.concat(i + ".jpg");
				imgSize++;
				DownLoadUtil.saveHtml(node.getNodeNo(), filePath);
			} else if (node.getNodeType().equals(Node.LINK_NODE_TYPE_HREF)) {
				result.add(node);
			}
		}
		System.out.println(url + "-----图片一共有：" + imgSize);
		return result;

	}

}
